package com.hmob.hmdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.hmob.hmsdk.ads.AdError;
import com.hmob.hmsdk.ads.nativ.HMNative;
import com.hmob.hmsdk.ads.nativ.NativeListener;
import com.hmob.hmsdk.entity.NativeResource;

import java.util.List;

public class TextLinkActivity extends AppCompatActivity {

    private List<NativeResource> mList;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_link);
        tvTitle = findViewById(R.id.title);
        fetchAd();
    }

    private void fetchAd() {
        //1、创建广告对象
        HMNative hmNative = new HMNative(TextLinkActivity.this, Constants.ADID_TEXT, new NativeListener() {
            @Override
            public void onADReceived(List<NativeResource> list) {
                mList = list;
                if (mList == null || mList.size() == 0)
                    return;
                //获取广告信息并展示
                final NativeResource nativeResource = mList.get(0);
                tvTitle.setText(nativeResource.getTitle());
                //展示成功后调用SDK方法反馈
                nativeResource.onExposed(tvTitle);
                //一定要先调用onExposed方法曝光广告，否则将无法调用onTouch方法
                tvTitle.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        nativeResource.onTouch(v, event);
                        return true;
                    }
                });
            }

            @Override
            public void onNoAD(AdError adError) {
                Log.e("NativeActivity", adError.getErrorMessage());
            }
        });
        //2、加载广告，可预加载多条，范围1-5，可能有重复广告
        hmNative.load(1);
    }
}
